﻿package com.athena.process
{
	import flash.events.*;
	import flash.display.*;
	import com.athena.vo.*;
	import com.athena.controller.*;
	import com.adobe.serialization.json.JSON;
	import flash.utils.*;
	/**
	 * ...
	 * @author ...
	 */
	public class EmotionFunction 
	{
		public static var isDisplayEmotion : Boolean = false ; //Kiem soat viec an hien bang chon Emotion
		public static var nameEmotion : String = "" ;		
		public static var xEmotion : Number = 0 ;
		public static var yEmotion : Number = 0 ;
		public static var x1EmotionG : Number = 715 ;
		public static var y1EmotionG : Number = 270 ;
		public static var x2EmotionG : Number = 715 ;
		public static var y2EmotionG : Number = 8 ;		
		public static var emotionTimer1:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var emotionTimer2:Timer = new Timer(1000);//Dieu khien dong ho chay		
		public static var emotionPosition : int = 0 ; //Vi tri hien thi Emotion 1==>8
		/****************************************** Emotion ***************************************************/
		public static function StartEmotion1(ev : TimerEvent) : void {
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			if (emotionTimer1.currentCount == emotionTimer1.repeatCount) {
				emotionTimer1.stop() ;
				var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion1")) ;
				while (mc != null) {
					Cocaro.mainStage_mc.removeChild(mc) ;
					mc = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion1")) ;
				}
			}
		}
		public static function StartEmotion2(ev : TimerEvent) : void {
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			if (emotionTimer2.currentCount == emotionTimer2.repeatCount) {
				emotionTimer2.stop() ;
				var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion2")) ;
				while (mc != null) {
					Cocaro.mainStage_mc.removeChild(mc) ;
					mc = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion2")) ;
				}
			}
		}		
		
		public static function Display_Emotion(type : String, xEmotion : Number, yEmotion : Number, name : String) {
			var mc : MovieClip ;
			if (type == "EA7") {				 
				mc = Cocaro.ArrEmotion[0] ;
			} else if (type == "EA8") {
				mc = Cocaro.ArrEmotion[1] ;
			} else if (type == "EA9") {
				mc = Cocaro.ArrEmotion[2] ;
			} else if (type == "EA10") {
				mc = Cocaro.ArrEmotion[3] ;
			} else if (type == "EA11") {
				mc = Cocaro.ArrEmotion[4] ;
			} else if (type == "EA12") {
				mc = Cocaro.ArrEmotion[5] ;
			} else if (type == "EA13") {
				mc = Cocaro.ArrEmotion[6] ;
			} else if (type == "EA14") {
				mc = Cocaro.ArrEmotion[7];
			} else if (type == "EA15") {
				mc = Cocaro.ArrEmotion[8] ;				
			}
			if (mc == null) return ;
			mc.x = xEmotion ;
			mc.y = yEmotion ;
			//tf.appendText("XY:" + xEmotion + "-" + yEmotion) ;
			mc.name = name ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}
		
		public static function Display_Emotion_Control(obj : Object) : void {
			//Cocaro.tf.appendText("==>Emotion" + obj.Name + "-" + obj.NName) ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			}	
			//var obj : Object = JSON.decode(str) ;
			var cs : int = 0 ;
			//{Xac dinh thang bi hien thi
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[i].PlayerName == obj.NName) {
					cs = i ;	
					break ;
				} 
			}
			//}
			var mcRemove : MovieClip ;
			//Cocaro.tf.appendText("cs:" + ProcessInTable.currPosition + "-" + cs) ;
			if (obj.NName != Cocaro.currPlayer.PlayerName) {
				xEmotion = x2EmotionG ;
				yEmotion = y2EmotionG ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion2")) ;
				if (mcRemove != null)
					Cocaro.mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimer2.running) 
					emotionTimer2.stop() ;					
				emotionTimer2 = new Timer(1000) ;
				emotionTimer2.repeatCount = 4 ;
				emotionTimer2.addEventListener(TimerEvent.TIMER, StartEmotion2);				
				emotionTimer2.start() ;
				Display_Emotion(obj.T, xEmotion, yEmotion, "Emotion2") ;
			} else {
				xEmotion = x1EmotionG ;
				yEmotion = y1EmotionG ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion1")) ;
				if (mcRemove != null)
					Cocaro.mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimer1.running) 
					emotionTimer1.stop() ;
				emotionTimer1 = new Timer(1000) ;
				emotionTimer1.repeatCount = 4 ;
				emotionTimer1.addEventListener(TimerEvent.TIMER, StartEmotion1);				
				emotionTimer1.start() ;
				Display_Emotion(obj.T, xEmotion, yEmotion, "Emotion1") ;
			}
			if (obj.Name != obj.NName) {
				Cocaro.mainStage_mc.Chat_mc.InTable_txt.htmlText += "<font color=\"#FFFFFF\">====>" + obj.Name + " Gửi " + obj.NName + "</font>\n" ;
				Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText += "<font color=\"#FFFFFF\">====>" + obj.Name + " Gửi " + obj.NName + "</font>\n" ;
				Cocaro.mainStage_mc.Chat_mc.InTable_txt.verticalScrollPosition = Cocaro.mainStage_mc.Chat_mc.InTable_txt.maxVerticalScrollPosition ;
				Cocaro.mainStage_mc.Chat_mc.All_txt.verticalScrollPosition = Cocaro.mainStage_mc.Chat_mc.All_txt.maxVerticalScrollPosition ;
			}
			xEmotion = 0 ;
			yEmotion = 0 ;
		}		
		
		public static function Emotion_Kick(type : String) : void {
			//tf.appendText("==>" + type) ;
			var object : Object = new Object() ;
			object.Name = Cocaro.currPlayer.PlayerName;
			object.NName = nameEmotion ;
			object.Data = "" ;
			object.evt = "chattable";
			object.T = type ;
			Cocaro.mainSocket.sendData(JSON.encode(object));
			//Cocaro.tf.appendText("==>SendEmotion:" + JSON.encode(object)) ;
			/*			
			object.ID = ProcessInTable.currTable.TableID ;
			object.NName = nameEmotion ;
			object.Type = type ;
			Cocaro.mainSocket.sendData("EA:" + JSON.encode(object)) ;*/
		}
		public static function Emotion0_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA7") ;
		}
		public static function Emotion1_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA8") ;
		}
		public static function Emotion2_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA9") ;
		}
		public static function Emotion3_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA10") ;
		}
		public static function Emotion4_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA11") ;
		}
		public static function Emotion5_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA12") ;
		}
		public static function Emotion6_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA13") ;
		}
		public static function Emotion7_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA14") ;
		}
		public static function Emotion8_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA15") ;
		}
		
		public static function mouseEmotionMoveOut(evt : MouseEvent) : void {			
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			if (!isDisplayEmotion) return ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			}
			isDisplayEmotion = false ;
		}
		public static function DisplayEmotionGroup(x : int, y: int) : void {
			var mc : MovieClip = MovieClip(new EmotionGroup()) ;
			mc.name = "EmotionGroup" ;
			mc.Emotion0_mc.addEventListener(MouseEvent.CLICK, Emotion0_Kick) ;
			mc.Emotion1_mc.addEventListener(MouseEvent.CLICK, Emotion1_Kick) ;
			mc.Emotion2_mc.addEventListener(MouseEvent.CLICK, Emotion2_Kick) ;
			mc.Emotion3_mc.addEventListener(MouseEvent.CLICK, Emotion3_Kick) ;
			mc.Emotion4_mc.addEventListener(MouseEvent.CLICK, Emotion4_Kick) ;
			mc.Emotion5_mc.addEventListener(MouseEvent.CLICK, Emotion5_Kick) ;
			mc.Emotion6_mc.addEventListener(MouseEvent.CLICK, Emotion6_Kick) ;
			mc.Emotion7_mc.addEventListener(MouseEvent.CLICK, Emotion7_Kick) ;
			mc.Emotion8_mc.addEventListener(MouseEvent.CLICK, Emotion8_Kick) ;
			mc.addEventListener(MouseEvent.ROLL_OUT, mouseEmotionMoveOut) ;
			isDisplayEmotion = true ;
			mc.x = x ;
			mc.y = y ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}		
		public static function Avatar1_Kick(evt : MouseEvent) : void {
			if (Cocaro.mainStage_mc.getChildByName("Emotion1") != null) return ;
			DisplayEmotionGroup(715, 310) ;
			nameEmotion = Cocaro.mainStage_mc.Player1_mc.Name_txt.text ;
		}	
		public static function Avatar2_Kick(evt : MouseEvent) : void {
			if (Cocaro.mainStage_mc.getChildByName("Emotion2") != null) return ;
			DisplayEmotionGroup(715, 40) ;
			nameEmotion = Cocaro.mainStage_mc.Player2_mc.Name_txt.text ;
		}	
		
		public static function RemoveEmotion() : void {
			var mcEmotion : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion4")) ;
			if (mcEmotion != null)
				Cocaro.mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion1")) ;
			if (mcEmotion != null)
				Cocaro.mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(Cocaro.mainStage_mc.getChildByName("Emotion2")) ;
			if (mcEmotion != null)
				Cocaro.mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mcEmotion != null) {
				Cocaro.mainStage_mc.removeChild(mcEmotion) ;
				mcEmotion = MovieClip(Cocaro.mainStage_mc.getChildByName("EmotionGroup")) ;
			}
		}
		/******************************************************************************************************/
	}
}